﻿/* Copyright © 2010 Nolics
Nolics.net and NSQLObjects are trademarks of Nolics.

Microsoft Public License (Ms-PL)

This license governs use of the accompanying software. If you use the software, you accept this license. If you do not accept the license, do not use the software.

1. Definitions
The terms "reproduce," "reproduction," "derivative works," and "distribution" have the same meaning here as under U.S. copyright law.
A "contribution" is the original software, or any additions or changes to the software.
A "contributor" is any person that distributes its contribution under this license.
"Licensed patents" are a contributor's patent claims that read directly on its contribution.

2. Grant of Rights
(A) Copyright Grant- Subject to the terms of this license, including the license conditions and limitations in section 3, each contributor grants you a non-exclusive, worldwide, royalty-free copyright license to reproduce its contribution, prepare derivative works of its contribution, and distribute its contribution or any derivative works that you create.
(B) Patent Grant- Subject to the terms of this license, including the license conditions and limitations in section 3, each contributor grants you a non-exclusive, worldwide, royalty-free license under its licensed patents to make, have made, use, sell, offer for sale, import, and/or otherwise dispose of its contribution in the software or derivative works of the contribution in the software.

3. Conditions and Limitations
(A) No Trademark License- This license does not grant you rights to use any contributors' name, logo, or trademarks.
(B) If you bring a patent claim against any contributor over patents that you claim are infringed by the software, your patent license from such contributor to the software ends automatically.
(C) If you distribute any portion of the software, you must retain all copyright, patent, trademark, and attribution notices that are present in the software.
(D) If you distribute any portion of the software in source code form, you may do so only under this license by including a complete copy of this license with your distribution. If you distribute any portion of the software in compiled or object code form, you may only do so under a license that complies with this license.
(E) The software is licensed "as-is." You bear the risk of using it. The contributors give no express warranties, guarantees or conditions. You may have additional consumer rights under your local laws which this license cannot change. To the extent permitted under your local laws, the contributors exclude the implied warranties of merchantability, fitness for a particular purpose and non-infringement. 
 */

using System;
using System.Data.SqlClient;

namespace NSQLObjects.Persistence
{
    /// <summary>
    /// Maps CLR types to sql server parameters
    /// </summary>
    public static class TypeMap
    {
        public static SqlParameter CreateParameter(this SqlCommand forCommand, string parameterName) {
            SqlParameter par = new SqlParameter();
            par.ParameterName = parameterName;
            forCommand.Parameters.Add(par);
            return par;
        }

        public static void FromSql(this SqlDataReader par, int index, out string str) {
            if (par.IsDBNull(index))
                str = null;
            else
                str = par.GetString(index);
        }

        public static void ToSql(this SqlParameter par, string value) {
            par.DbType = System.Data.DbType.String;
            if (value == null)
                par.Value = DBNull.Value;
            else
                par.Value = value;
        }


        public static void FromSql(this SqlDataReader par, int index, out long lval) {
            if (par.IsDBNull(index))
                lval = 0;
            else
                lval = par.GetInt64(index);
        }

        public static void ToSql(this SqlParameter par, long lval) {
            par.DbType = System.Data.DbType.Int64;
            par.Value = lval;
        }

        public static void FromSql(this SqlDataReader par, int index, out int ival) {
            if (par.IsDBNull(index))
                ival = 0;
            else
                ival = par.GetInt32(index);
        }

        public static void ToSql(this SqlParameter par, int ival) {
            par.DbType = System.Data.DbType.Int32;
            par.Value = ival;
        }


        public static void FromSql(this SqlDataReader par, int index, out int? ival) {
            if (par.IsDBNull(index))
                ival = null;
            else
                ival = par.GetInt32(index);
        }

        public static void ToSql(this SqlParameter par, int? ival) {
            par.DbType = System.Data.DbType.Int32;
            if (ival.HasValue)
                par.Value = ival.Value;
            else
                par.Value = DBNull.Value;
        }

        public static void FromSql(this SqlDataReader par, int index, out long? lval) {
            if (par.IsDBNull(index))
                lval = null;
            else
                lval = par.GetInt64(index);
        }

        public static void ToSql(this SqlParameter par, long? lval) {
            par.DbType = System.Data.DbType.Int64;
            if (lval.HasValue)
                par.Value = lval.Value;
            else
                par.Value = DBNull.Value;
        }

        public static void FromSql(this SqlDataReader par, int index, out double dval) {
            if (par.IsDBNull(index))
                dval = 0;
            else
                dval = par.GetDouble(index);
        }


        public static void ToSql(this SqlParameter par, double dval) {
            par.DbType = System.Data.DbType.Double;
            par.Value = dval;
        }

        public static void FromSql(this SqlDataReader par, int index, out double? dval) {
            if (par.IsDBNull(index))
                dval = null;
            else
                dval = par.GetDouble(index);
        }


        public static void ToSql(this SqlParameter par, double? dval) {
            par.DbType = System.Data.DbType.Double;
            if (dval.HasValue)
                par.Value = dval.Value;
            else
                par.Value = DBNull.Value;
        }

        public static void FromSql(this SqlDataReader par, int index, out DateTime dtVal) {
            if (par.IsDBNull(index))
                dtVal = new DateTime();
            else
                dtVal = par.GetDateTime(index);
        }

        public static void ToSql(this SqlParameter par, DateTime dtVal) {
            par.DbType = System.Data.DbType.DateTime;
            if (dtVal.Ticks == 0)
                par.Value = DBNull.Value;
            else
                par.Value = dtVal;
        }

        public static void FromSql(this SqlDataReader par, int index, out bool bVal) {
            if (par.IsDBNull(index))
                bVal = false;
            else
                bVal = par.GetBoolean(index);
        }

        public static void ToSql(this SqlParameter par, bool bVal) {
            par.DbType = System.Data.DbType.Boolean;
            par.Value = bVal;
        }

        public static void FromSql(this SqlDataReader par, int index, out Guid gVal) {
            if (par.IsDBNull(index))
                gVal = new Guid();
            else
                gVal = par.GetGuid(index);
        }

        public static void ToSql(this SqlParameter par, Guid gVal) {
            par.DbType = System.Data.DbType.Guid;
            if (gVal == Guid.Empty)
                par.Value = DBNull.Value;
            else
                par.Value = gVal;
        }

        public static void FromSql(this SqlDataReader par, int index, out decimal dVal) {
            if (par.IsDBNull(index))
                dVal = 0;
            else
                dVal = par.GetDecimal(index);
        }

        public static void ToSql(this SqlParameter par, decimal dVal) {
            par.DbType = System.Data.DbType.Decimal;
            par.Value = dVal;
        }

        public static void FromSql(this SqlDataReader par, int index, out decimal? dVal) {
            if (par.IsDBNull(index))
                dVal = null;
            else
                dVal = par.GetDecimal(index);
        }

        public static void ToSql(this SqlParameter par, decimal? dVal) {
            par.DbType = System.Data.DbType.Decimal;
            if (dVal.HasValue)
                par.Value = dVal.Value;
            else
                par.Value = DBNull.Value;
        }

        public static void FromSql(this SqlDataReader par, int index, out byte[] bVal) {
            if (par.IsDBNull(index))
                bVal = null;
            else {
                var tmp = par.GetSqlBinary(index);
                bVal = tmp.Value;
            }
        }

        public static void ToSql(this SqlParameter par, byte[] bVal) {
            par.DbType = System.Data.DbType.Binary;
            if (bVal != null)
                par.Value = bVal;
            else
                par.Value = DBNull.Value;
        }
    }
}
